package com.example.test.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.RemoteViews;

import com.example.test.R;
import com.example.test.entity.Song;
import com.example.test.util.MusicUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MusicService extends Service {

    //当前歌曲下标
    private int index = 0;

    private MediaPlayer mediaPlayer;
    private List<Song> songList;

    public MusicService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();
        songList = MusicUtils.getMusics(this);
    }

    public class MyBinder extends Binder{
        public void callplay(int position){
            play(position);
        }

        public void callpause(){
            pause();
        }

        public void callrestart(){
            restart();
        }

        public void callNext(){
            next();
        }

        public void callBack(){
            back();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    //发送一个前台的通知
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Notification.Builder builder = new Notification.Builder(this);

        builder.setSmallIcon(R.mipmap.ic_launcher);

        //自定义广播中的布局
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);

        //广播 暂停
        Intent intent1 = new Intent();
        intent1.setAction("com.notify.pause");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 111, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.noyify_pause,pendingIntent);
        //广播 继续
        Intent intent2 = new Intent();
        intent2.setAction("com.notify.restart");
        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 112, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.nofity_restart,pendingIntent1);
        //广播 上一首
        Intent intent3 = new Intent();
        intent3.setAction("com.notify.back");
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 113, intent3, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.notify_back,pendingIntent2);
        //广播 下一首
        Intent intent4 = new Intent();
        intent4.setAction("com.notify.next");
        PendingIntent pendingIntent3 = PendingIntent.getBroadcast(this, 114, intent4, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.notify_next,pendingIntent3);

        //设置通知的布局
        builder.setCustomContentView(remoteViews);

        //开始一个前台服务
        Notification build = builder.build();
        startForeground(1,build);

        return super.onStartCommand(intent, flags, startId);
    }

    //点击播放选中曲目
    public void play(int position){

        index = position;

        //恢复初始化
        mediaPlayer.reset();

        try {
            mediaPlayer.setDataSource(songList.get(position).getData());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //暂停
    public void pause(){
        if (mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }
    }

    //继续
    public void restart(){
        mediaPlayer.start();
    }

    //下一首
    public void next(){
        if (++index >= songList.size()){
            index = 0;
        }
        play(index);
    }

    //上一首
    public void back(){
        if (--index <= -1){
            index = songList.size()-1;
        }
        play(index);
    }
}